home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* collage.c
- * This program displays images at random positions in a window.
- * An option allows the images to be zoomed by a randow zoom
- * factor. Up to MAX_IMAGES can be read. To run, type:
- *
- * collage <imageFile1.rgb> [<imageFile2.rgb>] ...
- *
- * Try "collage /usr/demos/data/textures/*.rgb"
- *
- * Escape key - exit the program
- * SPACEBAR - toggle fixed/random zoom
- *
- * David Marsland, MTS, SGI Education R & D, 1993
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "rgbImageFile.h"
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( int );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- #define MAX_IMAGES 100
-
- typedef struct _image {
- GLuint *image;
- int width, height;
- } ImageInfo;
-
- static ImageInfo images[MAX_IMAGES];
- static ImageInfo *curImage = NULL;
- static GLint imageCount;
-
- static GLsizei winWidth, winHeight;
-
- static GLboolean zoomFlag = GL_FALSE;
-
- GLvoid
- main ( int argc, char *argv[] )
- {
- GLsizei width, height;
- int i;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf(stderr,
- "usage: %s <imageFile1> [<imageFile2> ... ]\n", argv[0] );
- exit (1);
- }
-
- imageCount = argc - 1;
- if (imageCount > MAX_IMAGES) imageCount = MAX_IMAGES;
-
- srand48( imageCount ); /* seed random number generator */
-
- /* read in all image files specifed as command line args */
- for ( i = 0; i < imageCount; i++ ) {
- images[i].image = rgbReadImageFile( argv[i+1],
- &images[i].width, &images[i].height );
- }
- curImage = &images[0];
-
- /* create a window that is 3/4 the size of the screen */
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- winWidth = 3*width/4;
- winHeight = 3*height/4;
- glutInitWindowPosition( width / 8, height / 8 );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n\n%s - creates a collage of images "
- "which can be zoomed\n\n"
- "Escape key - exit the program\n"
- "SPACEBAR - toggle fixed/random zoom\n\n",
- progname);
- }
-
- void
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case ' ':
- zoomFlag = !zoomFlag;
- if ( !zoomFlag )
- glPixelZoom( 1.0, 1.0 );
- glutPostRedisplay();
- break;
-
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width - 1, height - 1);
-
- winWidth = width;
- winHeight = height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- glClear( GL_COLOR_BUFFER_BIT );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the current image */
-
- curImage = &images[(int) (imageCount * drand48())];
-
- /* if zooming is enabled, randomly change the zoom factor */
- if ( zoomFlag ) {
- /* generates zoom values in the range [ -2.0, 2.0 ) */
- glPixelZoom( ( drand48() * 4.0 ) - 2.0,
- ( drand48() * 4.0 ) - 2.0 );
- }
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLint xPos, yPos;
-
- xPos = drand48()*winWidth;
- yPos = drand48()*winHeight;
- glRasterPos2i( xPos, yPos );
- glDrawPixels( curImage->width, curImage->height,
- GL_RGBA, GL_UNSIGNED_BYTE, curImage->image );
- glFlush();
- }
-